package cloud.forms;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import cloud.beans.Person;
import cloud.beans.Staff;
import cloud.dao.StaffDao;

public final class StaffForm extends Form{

	private static final String CHAMP_ANIMEID = "anime_id";
	private static final String CHAMP_PERSONID = "person_id";
	private static final String CHAMP_ROLE = "role";

	private StaffDao staffDao;
	private Staff staff;
	
	public StaffForm() {
		
		super();
		this.staffDao = new StaffDao();
		this.staff = new Staff();
	}
	
	
	public Staff createStaff( HttpServletRequest request ) {
	
		long anime_id = Long.valueOf(getValeurChamp( request, CHAMP_ANIMEID ));
		long person_id = Long.valueOf(getValeurChamp( request, CHAMP_PERSONID ));
		String role = getValeurChamp( request, CHAMP_ROLE );
		
		try {
			validationChampRequis(request.getParameter( "anime_id" ));
		}
		catch ( Exception e ) {
			setErreur( CHAMP_ANIMEID, e.getMessage() );
		}
		this.staff.setAnime_id( anime_id );
		
		
		try {
			validationChampRequis(request.getParameter( "person_id" ));
		}
		catch ( Exception e ) {
			setErreur( CHAMP_PERSONID, e.getMessage() );
		}
		this.staff.setPerson_id( person_id );
		
		
		try {
			validationChampRequis(role);
		}
		catch ( Exception e ) {
			setErreur( CHAMP_ROLE, e.getMessage() );
		}
		this.staff.setRole( role );
			
		if ( this.erreurs.isEmpty() ) {
			if (this.staffDao.creer(this.staff))
				resultat = "Success.";
			else {
				setErreur(null, "Error.");
				this.resultat = "Technical Error.";
			}
		} 
		else {
			this.resultat = "Error.";
		}

		return this.staff;
	}
	
	public List<Staff> getList(long id) {

		// Récuperation de la liste des codes en fonction de la rubrique
		List<Staff> staff = staffDao.lister( id );

		// Si la recherche retourne un resultat
		if ( staff != null && !staff.isEmpty() ) {
			resultat = "Liste récupérée.";
		}
		
		return staff;
	}
	
	public List<Staff> getListByPerson(long id) {

		// Récuperation de la liste des codes en fonction de la rubrique
		List<Staff> staff = staffDao.listerForPerson( id );

		// Si la recherche retourne un resultat
		if ( staff != null && !staff.isEmpty() ) {
			resultat = "Liste récupérée.";
		}
		
		return staff;
	}
}